//
//  WhereAmIViewController.m
//  iPhoneDev01
//
//  Created by smu on 11-7-14.
//  Copyright 2011年 duk. All rights reserved.
//

#import "WhereAmIViewController.h"


@implementation WhereAmIViewController
@synthesize locationManager;
@synthesize startingPoint;
@synthesize latitudeLabel;
@synthesize longitudeLabel;
@synthesize horizontalAccuracyLabel;
@synthesize altitudeLabel;
@synthesize verticalAccuracyLabel;
@synthesize distanceTraveledLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [locationManager release];
    [startingPoint release];
    [latitudeLabel release];
    [longitudeLabel release];
    [horizontalAccuracyLabel release];
    [altitudeLabel release];
    [verticalAccuracyLabel release];
    [distanceTraveledLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //分配并初始化一个CLLocationManager实例,将我们得控制器类指定为委托
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    //将所需得精度设置为可用得最佳精度
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // Location Manager实例开始提供位置更新
    [locationManager startUpdatingLocation];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    self.locationManager = nil;
    self.latitudeLabel = nil;
    self.longitudeLabel = nil;
    self.horizontalAccuracyLabel = nil;
    self.altitudeLabel = nil;
    self.verticalAccuracyLabel = nil;
    self.distanceTraveledLabel = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
//更新位置管理器
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
    if (startingPoint == nil) {
        self.startingPoint = newLocation;
    }
    //coordinate属性 存储纬度和经度
    NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g°",
                                newLocation.coordinate.latitude];
    latitudeLabel.text = latitudeString;
    [latitudeString release];
    NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g°",
                                   newLocation.coordinate.longitude];
    
    //HorizontalAccuracy属性描述以coordinate作为其中心得一个圆得半径
    NSString *horizontalAccuracyString = [[NSString alloc]
                                          initWithFormat:@"%gm", 
                                          newLocation.horizontalAccuracy];
    horizontalAccuracyLabel.text = horizontalAccuracyString;
    [horizontalAccuracyString release];
    
    //altitude 你在海平面以下或以上多少米
    NSString *altitudeString = [[NSString alloc] initWithFormat:@"%gm",
                                newLocation.altitude];
    
    altitudeLabel.text = altitudeString;
    [altitudeString release];
    
    //verticalAccuracy精确到海平面得精确程度
    NSString *verticalAccuracyString = [[NSString alloc]
                                        initWithFormat:@"%gm", 
                                        newLocation.verticalAccuracy];
    verticalAccuracyLabel.text = verticalAccuracyString;
    [verticalAccuracyString release];
    
    //确定两个CLLocation对象之间得距离
    CLLocationDistance distance = [newLocation getDistanceFrom:startingPoint];
    NSString *distanceString = [[NSString alloc] initWithFormat:@"%gm", distance];
    distanceTraveledLabel.text = distanceString;
    [distanceString release];
    
}   

//错误通知
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
    NSString *errorType = (error.code == kCLErrorDenied) ? 
    @"Access Denied" : @"Unknown Error";
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"Error getting Location" 
                          message:errorType 
                          delegate:nil 
                          cancelButtonTitle:@"Okay" 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
@end
